<?php

/**
 * @filename MemberExpire.php
 * @author qiankun<qiankun@dodoca.com>
 * @datetime 2017/7/5
 * @description 判断会员状态脚本
 */
class MemberExpire
{
    public static function init()
    {
        self::setNotMember();//付费会员会员更新为非会员
        self::setIsMember();//付费会员非会员更新为会员
        exit;
    }

    /**
     * @author qiankun
     * @Fuction name setNotMember
     * @description 付费会员会员更新为非会员
     */
    public static function setNotMember()
    {
        $userMemberModel = new U_MemberCardModel();
        $where = array(
            'pay_type' => array('in',array(3,4)),
            'status' => 1
        );
        $where['_complex'] = array(
            '_logic' => 'or',
            'pay_start_time' => array('gt',SYS_TIME),
            'pay_end_time' => array('lt',SYS_TIME),
        );

        $field = array('id','shop_id','seller_id','fans_id');
        $userMember = $userMemberModel->select($where,$field,100,'id DESC');//获得过期会员数据
        //付费会员不符合时间范围的更新为非会员
        if($userMember)
        {
            //优先更新shopFans表
        	$fans = array();
        	$err = array();
        	$memberIds = array();
            $shopFansModel = new U_ShopFansModel(1);
            foreach($userMember as $k => $v)
            {
                $shopFansModel->_name = 'ts_shop_fans_'.($v['seller_id']%1000);
                $data = array(
                    'is_member' => 0
                );
                $updateWhere = array(
                    'seller_id' => $v['seller_id'],
                    'shop_id' => $v['shop_id'],
                    'fans_id' => $v['fans_id']
                );
                
                $res = $shopFansModel->updateData($data,$updateWhere);
                if($res !== false)
                {
                	$fans[] = $v;
                	$memberIds[] = $v['id'];
                }
                else 
                {
                	$err[] = $v;
                }
                
            }
            Tool_Log::info('会员设置成非会员memberId','MemberExpire-setNotMember',array('fans'=>$fans,'err'=>$err,'member_ids'=>$memberIds));
            //更新user_member表
            if($memberIds)
            {
            	$data = array('status' => 2);
            	$userMemberModel->updateData($data,array('id'=>array('in',$memberIds)));
            }
            
        }
    }

    /**
     * @author qiankun
     * @Fuction name setIsMember
     * @description 付费会员非会员更新为会员
     */
    public static function setIsMember()
    {
        $userMemberModel = new U_MemberCardModel();
        $where = array(
            'status' => 2,
            'pay_type' => array('in',array(3,4)),
            'pay_start_time' => array('lt',SYS_TIME),
            'pay_end_time' => array('gt',SYS_TIME),
        );
        $field = array('id','shop_id','seller_id','fans_id');
        $userMember = $userMemberModel->select($where,$field,100,'id DESC');//获得有效会员数据
        if($userMember)
        {
            //优先更新shopFans表
            $fans = array();
            $err = array();
            $memberIds = array();
            $shopFansModel = new U_ShopFansModel(1);
            foreach($userMember as $k => $v)
            {
                $shopFansModel->_name = 'ts_shop_fans_'.($v['seller_id']%1000);
                $data = array(
                    'is_member' => 1
                );
                $updateWhere = array(
                    'seller_id' => $v['seller_id'],
                    'shop_id' => $v['shop_id'],
                    'fans_id' => $v['fans_id']
                );
                $res = $shopFansModel->updateData($data,$updateWhere);
                if($res !== false)
                {
                    $fans[] = $v;
                    $memberIds[] = $v['id'];
                }
                else
                {
                    $err[] = $v;
                }
            }
            Tool_Log::info('非会员更新为会员memberId','MemberExpire-setIsMember',array('fans'=>$fans,'err'=>$err,'member_ids'=>$memberIds));
            //更新user_member表
            if($memberIds)
            {
                $data = array('status' => 1);
                $userMemberModel->updateData($data,array('id'=>array('in',$memberIds)));
            }
        }
    }
}